package com.doudou.oauth.exception;

import com.doudou.commons.entity.Result;
import com.doudou.oauth.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 傻男人
 * @Date: 2020/5/20 14:32
 * @Version: 1.0
 * @Description: 自定义权限相关的异常
 */
@Slf4j
@Component
public class SimpleAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) throws IOException {
        log.info("无权限的异常信息为："+ex.getMessage());
        ex.printStackTrace();
        HttpUtils.writerError(new Result(HttpStatus.FORBIDDEN.value(),"没有权限"),response);
    }
}